package com.item.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.Page;
import com.base.dialect.PaginationSupport;
import com.base.util.JSONUtils;
import com.base.util.StringUtil;
import com.item.dao.model.Department;
import com.item.dao.model.DepartmentExample;
import com.item.exception.JSONRepUtils;
import com.item.service.DepartmentService;

@Controller
public class DepartmentController {
	@Autowired
	private DepartmentService departmentService;
	
	@RequestMapping("/listDepartment")
	@ResponseBody
	public String list(Page param, Department department, Integer isNotPage, Integer isM) throws Exception{
		Object objForSer = null;
		DepartmentExample example = new DepartmentExample();
		PaginationSupport.setContext(param.getPageStart(), param.getPageSize(), null);
		DepartmentExample.Criteria criteria = example.createCriteria();
		
		if(StringUtils.isNotBlank(department.getPid())) {
			criteria.andPidEqualTo(department.getPid());
		}
		example.setOrderByClause("code ASC");
		List<Department> list = departmentService.selectByExample(example);

		if(isNotPage != null && isNotPage == 1) {
			objForSer = list;
		} else {
			objForSer = new Page(PaginationSupport.getContext().getTotalCount(), list);
		}
		
		return JSONRepUtils.serialize(objForSer, isM);
	}
	
	@RequestMapping("/showlistDepartment")
	public String list() {
		return "department/departmentList";
	}
	
	@RequestMapping("/toEditDepartment")
	public String toEdit()throws Exception{
		return "department/departmentEdit";
	}
	
	@RequestMapping(value="/saveDepartment")
	@ResponseBody
	public String save(Department department, Integer isTop, HttpSession session) throws Exception{		
		try{
			if(StringUtil.isEmpty(department.getId())){
				department.setCode(departmentService.getLeastCode(department.getPid()));
				
				department.setCreateTime(new Date());
				departmentService.insertSelective(department);
			} else {
				department.setModifyTime(new Date());
				departmentService.updateByPrimaryKeySelective(department);
			}
		}catch(Exception e){
			e.printStackTrace();
			return "{\"success\":false}";
		}
		
		return "{\"success\":true}";
	}
	
	@RequestMapping(value="/findByIdDepartment")
	@ResponseBody
	public String findById(String id,HttpSession session)throws Exception{		
		Department n = departmentService.selectByPrimaryKey(id);

		return JSONUtils.serialize(n);
	}
	
	@RequestMapping("/delDepartment")
	@ResponseBody
	public String del(String id,String url,HttpSession session)throws Exception{
		String[] ids = id.split(",");
		int len = ids.length;
		for(int i = 0; i < len; i++) {
			departmentService.deleteByPrimaryKey(ids[i]);
		}
		return "{\"success\":true}";
	}
	
	
}

